package cn.gz.demo.ui;


import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import cn.gz.demo.R;
import cn.gz.demo.databinding.TextClickBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.LogUtil;


/**
 * Created by Liuyz on 2018/7/5.
 */

public class TextClickActivity extends BaseActivity {
    private TextClickBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_text_click);

        String msg1 = "感谢您选择并使用良策App的产品和服务。良策App尊重并保护用户隐私，请您认真、仔细阅读《用户使用协议》";
        String msg2 = "和《隐私声明》（一下简称“协议”），协议包含我们收集、存储、保护、使用和共享您的个人信息以及使用良策App的产品和服务等的条款。";

        SpannableString courseSpannable = new SpannableString(msg1);
        SpannableString lessonSpannable = new SpannableString(msg2);

        ClickableSpan courseSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(TextClickActivity.this, "触发点击事件!..........111.............", Toast.LENGTH_SHORT).show();
            }
        };
        ClickableSpan lessonSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(TextClickActivity.this, "触发点击事件!..........222.............", Toast.LENGTH_SHORT).show();
            }
        };

        courseSpannable.setSpan(courseSpan, 10, 15, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        lessonSpannable.setSpan(lessonSpan, 1, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#ff0000"));
        courseSpannable.setSpan(colorSpan1, 10, 15, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        BackgroundColorSpan backgroundColorSpan1 = new BackgroundColorSpan(Color.parseColor("#ffffff"));
        courseSpannable.setSpan(backgroundColorSpan1, 10, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(Color.parseColor("#ff0000"));
        lessonSpannable.setSpan(colorSpan2, 1, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        BackgroundColorSpan backgroundColorSpan2 = new BackgroundColorSpan(Color.parseColor("#ffffff"));
        lessonSpannable.setSpan(backgroundColorSpan2, 1, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        binding.iv.append(courseSpannable);
        binding.iv.append(lessonSpannable);
        binding.iv.setMovementMethod(LinkMovementMethod.getInstance());


        final SpannableStringBuilder style = new SpannableStringBuilder();

        //设置文字
        style.append("关于本活动更多规则，请点我查看");

        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(TextClickActivity.this, "触发点击事件!", Toast.LENGTH_SHORT).show();
            }
        };
        style.setSpan(clickableSpan, 11, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.iv1.setText(style);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF"));
        style.setSpan(foregroundColorSpan, 11, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

//        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#ffffff"));
//        style.setSpan(backgroundColorSpan, 11, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //配置给TextView
        binding.iv1.setMovementMethod(LinkMovementMethod.getInstance());
        binding.iv1.setText(style);
        binding.iv1.setBackgroundResource(R.color.transparent);

        binding.tv.setText("狗狗命名为企业后婆婆婆婆婆婆in going破哦婆婆破哦后婆婆婆婆说一声后婆婆说王守义十三香破婆婆婆婆外婆婆婆in ins我婆婆始于足下咯咯哦，诺婆婆哦婆婆说我，的，你民族魂魄going很美很后给您婆婆强迫症，得破功工作人员破后给您in going您破哦破in going您in破in going您哦后给您婆婆破图哦婆婆in所以我好，你破后给您破肉有人性吗破后送后膜婆婆，让我随心所欲f打字测试po");


        binding.iv2.setMaxLines(10);
        binding.iv2.setText("狗狗命名为企业后婆婆婆婆婆婆in going破哦婆婆破哦后婆婆婆婆说一声后婆婆说王守义十三香破婆婆婆婆外婆婆婆in ins我婆婆始于足下咯咯哦，诺婆婆哦婆婆说我，的，你民族魂魄going很美很后给您婆婆强迫症，");
        binding.iv2.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                binding.iv2.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int lines = binding.iv2.getLineCount();
                LogUtil.showLog(".............lines.........." + lines);
                if (lines > 2) {
                    binding.iv2.setMaxLines(2);
                }
            }
        });
    }
}
